package com.scaffold.photo.service;

import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import wangjg.commons.page.Page;

import com.scaffold.photo.dao.AlbumDAO;
import com.scaffold.photo.dao.PhotoDAO;
import com.scaffold.photo.entity.Photo;

@Service
@Transactional
public class PhotoService {

	@Autowired
	private PhotoDAO photoDAO;
	@Autowired
	private AlbumDAO albumDAO;

	public Photo getPhoto(Long id ) {
		return this.photoDAO.findById(id);
	}

	public void save(List<Photo> list) {
		for(Photo photo:list){
			this.save(photo);
		}
	}
	
	public void save(Photo photo){
		Date now = new Date();
		if(photo.getId()==null){
			photo.setCreated(now);
			photo.setModified(now);
			this.photoDAO.save(photo);
		}else{
			photo.setModified(now);
			this.photoDAO.update(photo);
		}
	}

	public void update(Photo photo){
		Date now = new Date();
		photo.setModified(now);
		this.photoDAO.update(photo);
	}

	public void delete(Photo photo){
		this.photoDAO.delete(photo);

	}

	public void batchDelete(String idseq) {
		String[] sa = idseq.split(",");
		for(String s:sa){
			Long id = Long.parseLong(s);
			this.photoDAO.delete(id);

		}
	}
	
	public List<Photo> findByAlbumId(Long albumId) {
		return this.photoDAO.findByAlbumId(albumId);
	}

	public List<Photo> pageQuery(Page page, Long albumId, String title, String path, String remark, String keyword) {
		return this.photoDAO.pageQuery(page, albumId, title, path, remark, keyword);
	}

}
